//
//  WQNavigationViewController.swift
//  WeiBo
//
//  Created by Xmen on 16/9/19.
//  Copyright © 2016年 111. All rights reserved.
//

import UIKit

class WQNavigationViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 边缘手势的代理
        self.interactivePopGestureRecognizer?.delegate = self
    }


    // 监听push的子视图及nav里面的控制器
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        // 获取子控制器
        print(viewControllers.count)
        // 如果数量大于0就不是根控制器
        if viewControllers.count > 0{
            // 拿到第二个控制器
            if viewControllers.count == 1{
                // 获取上一级的title
                let title = viewControllers.first!.title!
                
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }else{
                 viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
            }
        viewController.title = "当前显示的第\(viewControllers.count + 1)级控制器"
        
        }
        super.pushViewController(viewController, animated: animated)
    }
    func backAction()  {
        
        popViewController(animated: true)

}
    //  是否处理这次开始点击的手势
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        //  如果是根视图控制器的话,不处理边缘手势
        if viewControllers.count == 1 {
            return false
        }
        
        
        return true
}

}
